package com.dpiinc.TechPage.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.model.jbean.CountryBean;
import com.dpiinc.TechPage.user.utils.Constant;
import com.dpiinc.TechPage.user.utils.PhoneUtils;
import com.dpiinc.TechPage.user.utils.ToastUtils;
import com.dpiinc.TechPage.widget.ClearEditText;

import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.Nullable;

/**
 * Created by hu489_000 on 2018-06-24.
 */

public class PhoneInputActivity extends BaseActivity {

    public static final String LOGIN_PHONE = "login_phone";
    public static final String COUNTRY_NUMBER = "country_number";
    TextView btn_xieyi2;
    ClearEditText edit_telphone;
    Button btn_next;
    String mTelPhone;
    TextView country_region;
    TextView country_name;
    TextView country_number;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.phone_main);
        super.onCreate(savedInstanceState);

        country_region = findViewById(R.id.country_region);
        country_region.setText(R.string.country_region);
        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        country_region.measure(spec,spec);
        country_number = findViewById(R.id.country_number);
        country_number.setWidth(country_region.getMeasuredWidth());
        country_number.setText("+"+getResources().getStringArray(R.array.CommonCountry)[0].split(",")[0]);

        btn_xieyi2 = findViewById(R.id.btn_xieyi2);
        btn_xieyi2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              //  if("en".equals(getSharedPreferences("app_language",Context.MODE_PRIVATE).getString("lauType",Locale.getDefault().getLanguage()))){
                    CommonWebviewActivity.webviewEntrance(PhoneInputActivity.this, Constant.PROTOCOL_AGREEMENT, "Agreement");
              /*  }else{
                    CommonWebviewActivity.webviewEntrance(PhoneInputActivity.this, Constant.APP_PROTOCOL, "使用协议");
                }*/
            }
        });

        country_name = findViewById(R.id.country_name);
        country_name.setText(getResources().getStringArray(R.array.CommonCountry)[0].split(",")[1]);
        country_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(PhoneInputActivity.this, CountryPickerActivity.class),Constant.GET_COUNTRY);
            }
        });

        edit_telphone = findViewById(R.id.edit_telphone);
        edit_telphone.setmOnLengthListener(new ClearEditText.OnLengthListener() {
            @Override
            public void onLength(int lenght) {
                if("+86".equals(country_number.getText())){
                    if (lenght == 13 ) {
                        btn_next.setEnabled(true);
                    } else {
                        btn_next.setEnabled(false);
                    }
                }else {
                    btn_next.setEnabled(true);
                }

            }
        });

        btn_next = findViewById(R.id.btn_next);
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleBlankPhone();
                if("+86".equals(country_number.getText())){
                    if (!judgePhoneFormate()) {
                        return;
                    }
                }
                Intent iIntent = new Intent(PhoneInputActivity.this, VerifyCodeActivity.class);
                iIntent.putExtra(LOGIN_PHONE, mTelPhone);
                iIntent.putExtra(COUNTRY_NUMBER, country_number.getText().toString().replace("+",""));
                startActivity(iIntent);
                finish();
            }
        });

        handleKeyBoard();
    }

    private void handleBlankPhone() {
        String phone = edit_telphone.getText().toString();
        if (!TextUtils.isEmpty(phone)) {
            mTelPhone = phone.replace(" ", "");
        }
    }

    private boolean judgePhoneFormate() {
        boolean isPhoneNum = PhoneUtils.isMobileNO(mTelPhone);
        if (mTelPhone.equals("12312341231") || mTelPhone.equals("12312341232")
                || mTelPhone.equals("12312341233") || mTelPhone.equals("12312341234")|| mTelPhone.equals("15262840973")) {
            return true;
        }
        if (TextUtils.isEmpty(mTelPhone)) {
            ToastUtils.showTopTip(this, getString(R.string.mobile_not_null));
            return false;
        } else if (!isPhoneNum) {
            ToastUtils.showTopTip(this, getString(R.string.phone_formate_error));
            return false;
        }
        return true;
    }

    private void handleKeyBoard() {
        edit_telphone.setFocusable(true);
        edit_telphone.setFocusableInTouchMode(true);
        edit_telphone.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) edit_telphone.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(edit_telphone, 0);
                           }
                       },
                500);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case Constant.GET_COUNTRY:
                    String name=((CountryBean)data.getSerializableExtra("country")).getName();
                    if(name!= null) {
                        country_name.setText(name);
                    }
                    String number=((CountryBean)data.getSerializableExtra("country")).getNumber();
                    if(number!= null) {
                        country_number.setText("+"+number);
                    }
                    break;
            }
        }
    }
}
